package com.heima.controller;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.vos.SearchArticleVo;
import com.heima.service.SearchArticleService;
import org.elasticsearch.search.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author chenshan
 */
@RestController
@RequestMapping("api/v1/article/search")
public class SearchController {

    @Autowired
    private SearchArticleService searchService;

    @PostMapping("search")
    public ResponseResult searchArticle(@RequestBody UserSearchDto searchDto, HttpServletRequest request) throws IOException {
        String id = request.getHeader("id");
        return searchService.searchArticle(searchDto, id);
    }

    /**
     * 保存文章到ES
     *
     * @param searchArticleVo 文章信息
     * @return ResponseResult
     */
    @PostMapping("save")
    public ResponseResult saveArticle(@RequestBody SearchArticleVo searchArticleVo) throws IOException {
        return searchService.saveArticle(searchArticleVo);
    }
}
